package ru.reaperoq.inventory.screens.main.admin.objects

import LoadingStatus
import admin.AdminObjectsViewModel
import admin.models.AdminObjectsEvent
import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SecondaryTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import org.koin.androidx.compose.navigation.koinNavViewModel
import ru.reaperoq.inventory.components.lists.ComponentsList
import ru.reaperoq.inventory.components.lists.ComputersList
import kotlin.random.Random

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AdminObjectsScreen(
    modifier: Modifier = Modifier,
    viewModel: AdminObjectsViewModel = koinNavViewModel(),
    onOpenEdit: (Int?, String) -> Unit
) {
    var selectedTabIndex by remember { mutableIntStateOf(0) }
    val viewState by viewModel.viewStates().collectAsState()
    val context = LocalContext.current
    LaunchedEffect(Unit) {
        viewModel.obtainEvent(AdminObjectsEvent.LoadObjects)
    }
    LaunchedEffect(viewState.loadingStatus) {
        if (viewState.loadingStatus is LoadingStatus.Error) {
            val error = viewState.loadingStatus as LoadingStatus.Error
            Toast.makeText(context, error.message, Toast.LENGTH_SHORT).show()
        }
    }

    Scaffold(
        modifier = modifier.statusBarsPadding(),
        topBar = {
            SecondaryTabRow(selectedTabIndex = selectedTabIndex) {
                Tab(
                    text = {
                        Text(text = "Компоненты")
                    },
                    onClick = { selectedTabIndex = 0 },
                    selected = selectedTabIndex == 0
                )
                Tab(
                    text = {
                        Text(text = "Компьютеры")
                    },
                    onClick = { selectedTabIndex = 1 },
                    selected = selectedTabIndex == 1
                )
            }
        },
        floatingActionButton = {
            FloatingActionButton(onClick = {
                onOpenEdit(
                    null,
                    Random(System.currentTimeMillis()).nextInt().toString()
                )
            }) {
                Icon(imageVector = Icons.Default.Add, contentDescription = null)
            }
        }
    ) { padding ->
        Column(
            modifier = Modifier
                .consumeWindowInsets(padding)
                .padding(padding)
        ) {
            if (viewState.loadingStatus is LoadingStatus.Loading) {
                LinearProgressIndicator(Modifier.fillMaxWidth())
            }
            when (selectedTabIndex) {
                0 -> {
                    ComponentsList(
                        modifier = Modifier
                            .fillMaxSize(),
                        components = viewState.components,
                        onClick = { id ->
                            onOpenEdit(
                                id,
                                Random(System.currentTimeMillis()).nextInt().toString()
                            )
                        }
                    )
                }

                1 -> {
                    ComputersList(
                        modifier = Modifier
                            .fillMaxSize(),
                        computers = viewState.computers,
                        onClick = { id ->
                            onOpenEdit(
                                id,
                                Random(System.currentTimeMillis()).nextInt().toString()
                            )
                        }
                    )
                }
            }
        }

    }
}